package com.atguigu.juc;

import java.util.concurrent.atomic.AtomicInteger;

class T1{
   private   volatile int number = 0;

   public int getNumber(){
       return number;
   }

   public synchronized void setNumber(){
       number++;
   }

    AtomicInteger atomicInteger = new AtomicInteger();

    public AtomicInteger getAtomicInteger(){
        return  atomicInteger;
    }
    public void setAtomicInteger(){
        atomicInteger.getAndIncrement();
    }

}

public class CASDemo {
    public static void main(String[] args) {
      AtomicInteger atomicInteger = new AtomicInteger(5);
        System.out.println("atomicInteger.compareAndSet(5,100) = " + atomicInteger.compareAndSet(5, 100)+"\t"+atomicInteger);
        System.out.println("atomicInteger.compareAndSet(5,200) = " + atomicInteger.compareAndSet(5, 200)+"\t"+atomicInteger);
        T1 t1 = new T1();
        for (int i = 1; i <=100 ; i++) {
            new Thread(() -> {
                for (int j = 1; j <=100 ; j++) {
                    t1.setNumber();
                    t1.setAtomicInteger();
                } },String.valueOf(i)).start();
        }


        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t1.getNumber());
        System.out.println(t1.getAtomicInteger().get());
    }
}
